{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7a05ec23-0c6a-48b5-9b35-cff345c66285",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/combination-sum-ii\n",
    "\n",
    "\n",
    "Runtime: 588 ms, faster than 11.19% of C++ online submissions for Combination Sum II.\n",
    "Memory Usage: 341.1 MB, less than 5.51% of C++ online submissions for Combination Sum II.\n",
    "\n",
    "\n",
    "```c++\n",
    "#include <bits/stdc++.h>\n",
    "#include<numeric>\n",
    "\n",
    "using namespace std;\n",
    "\n",
    "class Solution {\n",
    "public:\n",
    "    vector<vector<int>> result;\n",
    "    int target;\n",
    "\n",
    "    void go_through(vector<int> this_list, vector<int> list_left) {\n",
    "      if (list_left.size() == 0) {\n",
    "        return;\n",
    "      } else {\n",
    "        vector<int> copy_of_list_left = list_left;\n",
    "        for (auto num : copy_of_list_left) {\n",
    "          vector<int> new_list = this_list;\n",
    "          new_list.push_back(num);\n",
    "          int s = accumulate(new_list.begin(), new_list.end(), 0);\n",
    "          if (s > target) {\n",
    "            return;\n",
    "          } else if (s == target) {\n",
    "            sort(new_list.begin(), new_list.end());\n",
    "            result.push_back(new_list);\n",
    "          } else {\n",
    "            vector<int> copy_of_list_left = list_left;\n",
    "            auto found = find (copy_of_list_left.begin(), copy_of_list_left.end(), num);\n",
    "            copy_of_list_left.erase(found);\n",
    "            go_through(new_list, copy_of_list_left);\n",
    "          }\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "\n",
    "    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {\n",
    "      //7:54\n",
    "      result.clear();\n",
    "      this->target = target;\n",
    "\n",
    "      if (accumulate(candidates.begin(),candidates.end(),0) < target) {\n",
    "        return result;\n",
    "      }\n",
    "\n",
    "      sort(candidates.begin(),candidates.end());\n",
    "      vector<int> temp;\n",
    "      go_through(temp, candidates);\n",
    "\n",
    "      vector<vector<int>> new_result;\n",
    "      for (auto l : result) {\n",
    "        if (find(new_result.begin(),new_result.end(), l) == new_result.end()) {\n",
    "          new_result.push_back(l);\n",
    "        }\n",
    "      }\n",
    "\n",
    "      return new_result;\n",
    "      //8:12\n",
    "      //debug until 8:19\n",
    "    }\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1569df6-3ce3-4953-b2b6-96f7ddcbc728",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "name": ""
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
